package com.lushu.chapitre1.graphics.elements {
import com.sfdk.graphics.data.LayeredBitmap;
import com.sfdk.graphics.engine.api.IGraphicElement;
import com.sfdk.pools.LayeredBitmapPool;

import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Point;

public class MultiLayerElement implements IGraphicElement {
    public function MultiLayerElement(layeredBitmapType:String) {
        _layeredBitmapType = layeredBitmapType;
        initElement();
    }

    protected function initElement():void {
        _position = new Point();
        _layeredBitmap = LayeredBitmapPool.getFromPool(_layeredBitmapType);
        if (!_layeredBitmap.isLoaded) {
            _layeredBitmap.addEventListener("layerLoaded", onLayerLoaded);
        }
        else {
            onLayerLoaded();
        }
    }

    // ------------------------------------------ //
    //											  //
    // 				 proprerties				  //
    //											  //
    // ------------------------------------------ //

    private var _layeredBitmapType:String;
    private var _layeredBitmap:LayeredBitmap;
    private var _position:Point;
    private var _currentLayer:BitmapData;
    private var _currentOffsetX:int;
    private var _currentOffsetY:int;

    // ------------------------------------------ //
    //											  //
    // 				   methods					  //
    //											  //
    // ------------------------------------------ //

    protected var _id:int = 0;

    public function set id(value:int):void {
        _id = value;
    }

    public function get id():int {
        return _id;
    }

    public function get view():BitmapData {
        return _currentLayer;
    }

    public function get position():Point {
        _position.x = _x;
        _position.y = _y;
        return _position;
    }

    public function get tilePosition():Point {
        _position.x = _x;
        _position.y = _y;
        return _position;
    }

    protected var _x:Number = 0;

    public function set x(value:Number):void {
        _hasMoved = true;
        _x = int(value * 1000) / 1000;
    }

    public function get x():Number {
        return _x;
    }


    protected var _y:Number = 0;

    public final function get y():Number {
        return _y;
    }

    public final function set y(value:Number):void {
        _hasMoved = true;
        _y = int(value * 1000) / 1000;
    }

    private var _hasMoved:Boolean;

    public function get hasMoved():Boolean {
        return _hasMoved;
    }

    public function set hasMoved(value:Boolean):void {
        _hasMoved = value;
    }


    private var _hasChanged:Boolean

    public function get hasChanged():Boolean {
        return _hasChanged;
    }

    public function set hasChanged(value:Boolean):void {
        _hasChanged = value;
    }


    public function get isOnScreen():Boolean {
        if (!_layeredBitmap.isLoaded)
            return false;
        return true;
    }

    public function set isOnScreen(value:Boolean):void {
    }

    public function update():Boolean {
        return true;
    }

    // ------------------------------------------ //
    //											  //
    // 				event handlers	  			  //
    //											  //
    // ------------------------------------------ //

    protected function onLayerLoaded(e:Event = null):void {
        _currentLayer = _layeredBitmap.layeredData[0]
    }
}


}